#ifndef SLICE_H
#define SLICE_H

#include <assert.h>
#include <memory>
#include <string.h>
#include <string>


class Slice {
 private:
  const char* data_;
  int size_;

 public:
  Slice(/* args */) : data_(""), size_(0) {}
  Slice(const char* d, size_t n) : data_(d), size_(n) {}
  Slice(const std::string& s) : data_(s.data()), size_(s.size()) {}
  Slice(const char* s) : data_(s), size_(strlen(s)) {}
  Slice(const Slice&) = default;
  Slice& operator=(const Slice&) = default;
  const char* data() const { return data_; }
  size_t size() { return size_; }
  bool empty() { return size_ == 0; }
  char operator[](size_t n) const {
    assert(n < size_);
    return data_[n];
  }
  int compare(const Slice& b) const;
  ~Slice() {}
};

inline bool operator==(const Slice& x, const Slice& y) {
  return ((x.size() == y.size()) && (memcmp(x.data(), y.data(), x.size())));
}

inline bool operator!=(const Slice& x, const Slice& y) { return !(x == y); }

inline int Slice::compare(const Slice& b) const {
  const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
  int r = memcmp(data_, b.data(), min_len);
  if (r == 0) {
    if (size_ < b.size())
      r = -1;
    else if (size_ > b.size())
      r = +1;
  }
  return r;
}

#endif